package com.kzd.gtgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.gtgame.R
import com.kzd.gtgame.entity.LotteryConfig
import com.kzd.gtgame.entity.LotteryRecord
import com.kzd.gtgame.main.toActivity
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper


class LotteryConPopup(context: Context) :
    BasePopupWindow(context) {

    init {
        popupGravity = Gravity.CENTER
        setOverlayNavigationBar(false)
    }


    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_lottery_con)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)

        contentView.findViewById<ImageView>(R.id.ivLotteryConClose)
            .setOnClickListener {
                dismiss()
            }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.IN)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.OUT)
            .toDismiss()
    }

    fun setContent(content: String): LotteryConPopup {
        contentView.findViewById<RecyclerView>(R.id.rvLotteryConBody)
            .adapter = LotteryTextAdapter().apply {
            setNewInstance(mutableListOf(content))
        }
        return this
    }

    fun setContent(content: MutableList<LotteryConfig>): LotteryConPopup {
        contentView.findViewById<RecyclerView>(R.id.rvLotteryConBody)
            .adapter = LotteryCateAdapter().apply {
            setNewInstance(content)
            setOnItemChildClickListener { _, view, position ->
                if (view.id != R.id.ivLotteryCateGoto) {
                    return@setOnItemChildClickListener
                }
                val itemData = getItem(position)
                if (itemData.page.isNullOrEmpty()) {
                    return@setOnItemChildClickListener
                }
                toActivity(itemData.page, itemData.login)
            }
        }
        return this
    }

    fun setContentRecord(content: MutableList<LotteryRecord>): LotteryConPopup {
        contentView.findViewById<RecyclerView>(R.id.rvLotteryConBody)
            .adapter = LotteryRecordAdapter().apply {
            setNewInstance(content)
        }
        return this
    }

    fun setBackgroundImage(@DrawableRes resId: Int): LotteryConPopup {
        contentView.findViewById<ImageView>(R.id.ivLotteryConBackground)
            .setImageResource(resId)
        return this
    }

    private class LotteryCateAdapter :
        BaseQuickAdapter<LotteryConfig, BaseViewHolder>(R.layout.item_lottery_cate) {

        init {
            addChildClickViewIds(R.id.ivLotteryCateGoto)
        }

        override fun convert(holder: BaseViewHolder, item: LotteryConfig) {
            holder.setText(R.id.tvLotteryCateName, item.name)
                .setText(R.id.tvLotteryCateSub, item.title)
                .setText(R.id.tvLotteryCateNum, item.showNum)
        }
    }

    private class LotteryTextAdapter :
        BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_lottery_text) {
        override fun convert(holder: BaseViewHolder, item: String) {
            holder.setText(R.id.tvLotteryTextContent, item)
        }
    }

    private class LotteryRecordAdapter :
        BaseQuickAdapter<LotteryRecord, BaseViewHolder>(R.layout.item_lottery_record) {
        override fun convert(holder: BaseViewHolder, item: LotteryRecord) {
            val time =
                TimeUtils.millis2String(TimeUtils.string2Millis(item.create_at), "yyyy/MM/dd")
            holder.setText(R.id.tvLotteryRecordTime, time)
                .setText(R.id.tvLotteryRecordName, item.awards_name)
        }
    }

}